/**************************************************************
  > File Name: mplayer.c
  > Author: LuoLiang
  > Mail: 1204553475@qq.com 
  > Created Time: 2022年02月23日 星期三 11时44分31秒
 **************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

#define PATHNAME "music.fifo"

int fdm;

void handler(int s)
{
	int status;
	wait(&status);
	if (WIFEXITED(status))
	{
		printf("child is normel over!\n");
	}
	else if (WIFSIGNALED(status))
	{
		printf("child is anormel over!\n");
	}

}


int main(int argc, char **argv)
{
		
	unlink(PATHNAME);
	if (mkfifo(PATHNAME, 0666) < 0)
	{
		perror("mkfifo()");
		exit(1);
	}

	pid_t pid;
	int pfd[2];
	if (pipe(pfd) < 0)
	{
		perror("pipe()");
		exit(1);
	}
	signal(SIGCHLD, handler);		
	pid = fork();
	if (pid < 0)
	{
		perror("fork()");
		exit(1);
	}
	if (pid == 0)
	{
//		close(pfd[0]);
//		dup2(pfd[1], 1);
		int fd;
		fd = open("/dev/null", O_RDWR);
		if (fd < 0)
		{
			perror("open()");
			exit(1);
		}
		dup2(fd, 0);
		dup2(fd, 1);
		dup2(fd, 2);
		if (fd > 2)
			close(fd);

		execlp("mplayer", "mplayer", "-slave", "-quiet",\
			"-input", "file=./music.fifo", "/home/ll/Music/醒着醉.mp3", NULL);
		perror("execlp()");	
		exit(1);
	}
	else
	{

		printf("*******\n");
		fdm = open(PATHNAME, O_WRONLY);
		if (fdm < 0)
		{
			perror("open()");
			exit(1);
		}
/*		
		sleep(5);
		write(fd, "loadfile /home/ll/Music/123.mp3\n", strlen("loadfile /home/ll/Music/123.mp3\n"));
		sleep(5);
		write(fd, "pause\n", strlen("pause\n"));
		sleep(5);
		write(fd, "pause\n", strlen("pause\n"));
		close(fd);
		wait(NULL);
*/

	getchar();
	}
    exit(0);
}
